package cn.zktr.Personnel.model.service.zhouyongcong.end;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;

import cn.zktr.Personnel.common.Md5Encode;
import cn.zktr.Personnel.common.TimeStamp;
import cn.zktr.Personnel.model.pojos.PerPmsEmp;
import cn.zktr.Personnel.model.pojos.PerPmsHCont;
import cn.zktr.Personnel.model.pojos.PerPmsPot;
import cn.zktr.Personnel.model.pojos.PerPmsYG;
import cn.zktr.Personnel.model.pojos.PerPmsZG;
import cn.zktr.Personnel.model.pojos.PerPmsZQ;
import cn.zktr.Personnel.model.service.tanyali.EmaDaService;
import cn.zktr.Personnel.model.service.tanyali.EmaOutService;
import cn.zktr.Personnel.model.service.zhouyongcong.boolService;
import cn.zktr.Personnel.model.service.zhouyongcong.findService;
import cn.zktr.Personnel.model.service.zhouyongcong.savePojosService;
import cn.zktr.Personnel.model.service.zhouyongcong.saveService;

@Service
@Transactional(rollbackFor=Exception.class)
public class hopaService {
	
	@Autowired
	private EmaOutService out;
	@Autowired
	private saveService ss;
	@Autowired
	private savePojosService sps;
	@Autowired
	private EmaDaService daS;
	@Autowired
	private findService fs;
	@Autowired
	private boolService bl;
	
	public int login(HttpSession session,String pwd,String acc) {
		if(acc==null||acc=="") {
			return -1;
		}
		if(pwd==null||pwd==""||pwd.equals("")) {
			session.setAttribute("id",-2);
			session.setAttribute("ce_shi",acc);
			return -2;
		}
		int id = bl.bool_Act(acc,new Md5Encode().MD5Encode(pwd));
		PerPmsEmp ppe = fs.findEmpid(id);
		if(ppe.getPerPmsYGs().size()==0) {
			System.out.println("没有权限");
			setY(ppe);
		}else {
			for(PerPmsYG ppyg:ppe.getPerPmsYGs()) {
				if(ppyg.getPerPmsFun().getFunname().equals("登录")) {
					if(ppyg.getYgwht().equals("false")) {
						id = -3;
					}
				}
			}
		}
		session.setAttribute("id",id);
		return id;
	}
	
	public void setY(PerPmsEmp ppe) {
		ppe.setEmptime(new TimeStamp().getNetworkTime());
		sps.modif_Emp(ppe);
		PerPmsPot ppp = ppe.getPerPmsStms().get(0).getPerPmsPot();
		PerPmsHCont pphc = ppp.getPerPmsZhiji().getPerPmsHConts().get(0);
		ss.save_HCes(bl.bool_HCes()+1, ppe.getEmpid(), pphc.getContid());
		for(PerPmsZQ ppyq : ppp.getPerPmsZQs()) {
			ss.save_YQ(bl.bool_YQ()+1, ppe, ppyq.getPerPmsPe(), ppyq.getZqwht());
		}
		for(PerPmsZG ppyg : ppp.getPerPmsZGs()) {
			ss.save_YG(bl.bool_YG()+1, ppe, ppyg.getPerPmsFun(), ppyg.getZgwht());
		}
	}
	
	public String home_pape(HttpSession session,HttpServletRequest request,Object obj) {
		
		if(obj==null||obj=="") {
			return "login";
		}else {
			int id = Integer.valueOf(obj+"");
			if(id==-2) {
				try {
					if(daS.KaoShiQianZhi(session.getAttribute("ce_shi")+"")) {
						JSONArray SJ = out.selectSJ(session.getAttribute("ce_shi")+"");
						request.setAttribute("ShiJuanXX", SJ);
						return "tyl/shi-juan";
					}
				} catch (Exception e) {
					return "login";
				}
			}else if(id!=-1&&id!=-2&&id!=-3) {
				ss.save_Login(id,request);
				session.setAttribute("hopa", fs.findEmpid(id));
				return "home-pape";
			}
		}
		return "login";
	}
}
